package com.cat.model.biz.logic.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Service;

import com.alonew.core.commons.newstore.AbstractStoreConfig;
import com.alonew.core.commons.newstore.Store;
import com.alonew.core.commons.newstore.StoreConfig;
import com.alonew.core.commons.newstore.StoreFactory;
import com.cat.model.biz.logic.TestLogic;
import com.cat.model.common.util.StoreKey;
@Service("testLogic")
public class TestLogicImpl implements TestLogic {

	StoreKey key = StoreKey.TEST;
	
	private StoreConfig<TestCache> config = new AbstractStoreConfig<TestCache>(key.getValue()) {
		
		public TestCache buildCache() throws Exception {
			System.out.println("BlessPhrase build cache");
			List<String> list = new ArrayList<String>();
			Map<String, String> map = new HashMap<String, String>();
			TestCache testCache = TestCache.buildCache(list, map);
			return testCache;
		}

		
		public TestCache defaultCache() {
			return TestCache.defaultCache();
		}

		
		public long getReloadPeriod() {
			return TimeUnit.MINUTES.toMillis(30);
		}

	};

	private Store<TestCache> store = StoreFactory.createCopyOnWriteStore(config);

	
	public void flushCache() {
		key.flushAll(null);
	}

	
	public String get(String id) {
		return map().get(id);
	}

	
	public List<String> list() {
		TestCache cache = store.cache();
		if(cache != null){
			List<String> list = cache.list();
			if(list != null){
				return list;
			}
		}
		return Collections.emptyList();
	}

	
	public Map<String, String> map() {
		TestCache cache = store.cache();
		if(cache != null){
			Map<String, String> map = cache.map();
			if(map != null){
				return map;
			}
		}
		return Collections.emptyMap();
	}

	
	
}
